package problem17_19;

//面试题 17.19 消失得两个数字
//https://leetcode.cn/problems/missing-two-lcci/

class Solution {
    public int[] missingTwo(int[] nums) {
        int n = nums.length;
        int x = 0;
        for(int i = 1; i<=n+2; i++) x ^= i;
        for(int k : nums) x ^= k;
        // x = ret1 ^ ret2
        //获取x第一个值为1比特位
        int bitMap = x&(-x);
        int[] ret = new int[2];
        for(int i = 1; i<= n+2; i++) {
            if((bitMap & i) == 0) ret[0] ^= i;
            else ret[1] ^= i;
        }
        for(int k : nums) {
            if((bitMap & k) == 0) ret[0] ^= k;
            else ret[1] ^= k;
        }
        return ret;
    }
}